<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Peserta extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
        $this->load->library('session');
        //$this->load->library('email');
        $this->load->library('form_validation');
        $this->load->model('peserta_model');
        $this->load->model('batch_model');
        $this->load->model('kota_model');
		// $this->load->model('transaksi_keuangan_model');
        // $this->load->model('transaksi_akun_model');
		//$this->cek_login();
    }

	
	function cek_login(){
		$id_hak = $this->session->userdata('id_hak');
		if($id_hak < 1 || $id_hak > 2 || $id_hak == ''){
			redirect ('login');
		}
		
	}
	
    //load Grid
    function index() {
        $id_batch = $this->input->post('batch');
        if ($id_batch == null) {
            if ($this->session->userdata('id_batch') == null) {
                $id_batch = 0;
            } else {
                $id_batch = $this->session->userdata('id_batch');
            }
        }
        $this->daftar($id_batch);
    }

    function daftar($id_batch) {
        $this->session->set_userdata('id_batch', $id_batch);

        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Id_Peserta'] = array('No Peserta', 70, TRUE, 'center', 1);
        $colModel['Nama_Peserta'] = array('Nama Peserta', 120, TRUE, 'center', 1);
        $colModel['No_HP'] = array('No HP', 75, TRUE, 'center', 1);
        $colModel['Pekerjaan'] = array('Pekerjaan', 100, TRUE, 'center', 1);
        $colModel['Jenis_Usaha'] = array('Jenis Usaha', 100, TRUE, 'center', 1);
        $colModel['Omset'] = array('Omset', 80, TRUE, 'left', 0);
        //$colModel['No_Transfer'] = array('No Pembayaran', 75, TRUE, 'left', 0);
        $colModel['Tanggal_Pembayaran'] = array('Tanggal Pembayaran', 100, TRUE, 'center', 0);
        $colModel['Status'] = array('Status Fix', 60, TRUE, 'center', 0);
        $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);

        //get nama batch
        $nama_batch = '-';
        $data_batch = $this->batch_model->selectone($id_batch);
        foreach ($data_batch->result() as $row) {
            $nama_batch = $row->NAMA_BATCH;
        }

        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Peserta ' . $nama_batch,
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Tambah', 'add', 'spt_js');
        $buttons[] = array('separator');
        $buttons[] = array('Hapus', 'delete', 'spt_js');

        // mengambil data dari file controler ajax pada method grid_berkas
        $grid_js = build_grid_js('flex1', site_url("peserta/peserta/grid_peserta/" . $id_batch), $colModel, 'Nama_Peserta', 'asc', $gridParams, $buttons);
        //'<a href=\''.base_url().'index.php/peserta/peserta/status/'.$row->ID_PESERTA.'\'>'.$status.'</a> ',
        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/peserta/peserta/form_peserta/" . $id_batch . "'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data peserta?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/peserta/peserta/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";


        $data_grid['js_grid'] = $grid_js;

        //get data batch
        $data['id_batch'] = $id_batch;
        // if($this->session->userdata('id_hak')==1){
            // $data['batch'] = $this->batch_model->get_all_batch();
        // }
        // else{
            $data['batch'] = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
        //}

        //remdering view
        $data['title'] = 'Daftar Peserta';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'peserta/grid_peserta', $data_grid);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    function grid_peserta($id_batch) {
        $valid_fields = array('Id_Peserta','Nama_Peserta', 'No_HP', 'Pekerjaan', 'Jenis_Usaha', 'Omset', 'No_Transfer', 'Tanggal_Pembayaran', 'Status');
        $this->flexigrid->validate_post('Nama_Peserta', 'asc', $valid_fields);
        $records = $this->peserta_model->get_data_peserta($id_batch);
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            //set status peserta
            $status = '';
            if ($row->JUMLAH == NULL) {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/not_yet.png\'>';
            } else {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/fix.png\'>';
            }

			//modifikasi tanggal pembayaran
			$tanggal = substr($row->TANGGAL_PEMBAYARAN, -2);
			$bulan = substr($row->TANGGAL_PEMBAYARAN, -5, 2);
			$tahun = substr($row->TANGGAL_PEMBAYARAN, -10, 4);
			$tanggal_pembayaran = '' . $tanggal . '-' . $bulan . '-' . $tahun . '';
			
            $record_items[] = array(
                $row->ID_PESERTA,
                $no = $no + 1,
				$row->ID_PESERTA,
                $row->NAMA_PESERTA,
                //$row->JENIS_KELAMIN,
                //$row->TEMPAT_LAHIR,
                $row->NO_HP,
                $row->PEKERJAAN,
                $row->JENIS_USAHA,
				'Rp. '. number_format($row->OMSET, null, ',', '.'). ',-',
                //'Rp. ' . $row->OMSET . ',-',
                //$status,
                //$row->NO_TRANSFER,
				//$row->TANGGAL_PEMBAYARAN,
				$tanggal_pembayaran,
                '<a href=\'' . base_url() . 'index.php/peserta/peserta/form_pembayaran/' . $row->ID_PESERTA . '\'>' . $status . '</a> ',
                '<a href=\'' . base_url() . 'index.php/peserta/peserta/edit/' . $row->ID_PESERTA . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    public function form_peserta($id_batch) {
        $data['status'] = 'new';
        $data['title'] = 'Tambah Peserta';
        $data['batch'] = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
        $data['kota'] = $this->kota_model->get_all_kota();
        $data['id_batch'] = $id_batch;
        $data['back_link'] = '' . base_url() . 'index.php/peserta/peserta/daftar/' . $id_batch . '';
        $this->session->set_userdata('id_batch', $id_batch);

        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'peserta/form_peserta', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    public function form_peserta_umum($id_batch) {
        $data['status'] = 'new';
        $data['title'] = 'Tambah Peserta';
        $data['batch'] = $this->batch_model->get_all_batch();
        $data['kota'] = $this->kota_model->get_all_kota();
        $data['id_batch'] = $id_batch;
        $data['back_link'] = '' . base_url() . 'index.php/swalayan_home';
        $this->session->set_userdata('id_batch', $id_batch);

        $this->template->write_view('content', 'peserta/form_peserta', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    public function insert_peserta() {
        if ($this->cek_validasi() == FALSE) {
            if ($this->session->userdata('id_lisensi') == NULL) {
                $this->form_peserta_umum($this->session->userdata('id_batch'));
            } else {
                $this->form_peserta($this->session->userdata('id_batch'));
            }
        } else {
            $tahun = $this->input->post('thn');
            $bulan = $this->input->post('bln');
            $tanggal = $this->input->post('tgl');
            $tanggal_lahir = '' . $tahun . '-' . $bulan . '-' . $tanggal . '';
			$email = $this->input->post('email');
            $data = array(
                'NAMA_PESERTA' => $this->input->post('nama_peserta'),
                'NAMA_PANGGILAN' => $this->input->post('nama_panggilan'),
                'JENIS_KELAMIN' => $this->input->post('jenis_kelamin'),
                'TEMPAT_LAHIR' => $this->input->post('tempat_lahir'),
                'TANGGAL_LAHIR' => $tanggal_lahir,
                'NAMA_USAHA' => $this->input->post('nama_usaha'),
                'JENIS_USAHA' => $this->input->post('jenis_usaha'),
                'OMSET' => $this->input->post('omset'),
                'PEKERJAAN' => $this->input->post('pekerjaan'),
                'NO_HP' => $this->input->post('no_hp'),
                'NO_DARURAT' => $this->input->post('no_darurat'),
                'HUB_DARURAT' => $this->input->post('hub_darurat'),
                'EMAIL' => $this->input->post('email'),
                'TWITTER' => $this->input->post('twitter'),
                'UKURAN_BAJU' => $this->input->post('ukuran_baju'),
                'ALAMAT' => $this->input->post('alamat'),
                'KODE_POS' => $this->input->post('kode_pos'),
                'ID_KOTA' => $this->input->post('id_kota'),
                'REFERENSI' => $this->input->post('referensi'),
                'NO_TRANSFER' => $this->input->post('no_transfer'),
                'TANGGAL_PEMBAYARAN' => $this->input->post('tanggal_pembayaran'),
                'PROMO' => $this->input->post('promo'),
                'INFORMASI' => $this->input->post('informasi'),
                'ID_BATCH' => $this->input->post('batch'),
                'STATUS_PESERTA' => 0, // 0 = pesertta bulum fix
                'STATUS' => 1
            );
            
			//get nama batch
			$id_batch = $this->input->post('batch');
			$record_peserta = $this->batch_model->selectone($id_batch);
			foreach ($record_peserta->result() as $peserta) {
				$nama_batch = $peserta->NAMA_BATCH;
				$jam = $peserta->JAM;
				$tempat = $peserta->TEMPAT;
				$tanggal_mulai = $peserta->TANGGAL_ENTRY;
			}
			
			//START KIRIM EMAIL NOTIFIKASI KE PESERTA
			$config['protocol']		= 'smtp';
			$config['smtp_host']	= 'ssl://smtp.googlemail.com';
			$config['smtp_port']	= 465;//465
			$config['smtp_user']	= 'fiian91@gmail.com';
			$config['smtp_pass']	= '18mei1991';
			$this->load->library('email', $config);
			$this->email->set_newline("\r\n");
			//$message	= "Permintaan Anda sudah kami terima.\nSilahkan Lakukan pembayaran sesuai jumlah modul yang dipesan.\na. Invoice ini hanya berlaku seminggu, harap transfer dalam seminggu ini.\nb. Bila mungkin, harap mengisi kolom keterangan transfer dengan UserID referral.\n\n\nTerimakasih.";
			$message = "Selamat Anda sudah sukses melakukan registrasi pelatihan E-Camp Indonesia.\nSegera melakukan pembayaran cash ke kantor E-Camp terdekat atau transer ke:\nNo Rekening : 03125512\nAtas Nama : Budi\n\nPembayaran palinglambat 2 hari sebelum penyelenggaraan pelatihan.\nBerikut adalah data identitas yang Anda masukkan:\n\nNama : ".$this->input->post('nama_peserta')."\nJenis Kelamin : ".$this->input->post('jenis_kelamin')."\nNo HP : ".$this->input->post('no_hp')."\nEmail : ".$this->input->post('email')."\nUkuran Baju : ".$this->input->post('ukuran_baju')."\nBatch : ".$nama_batch."\nTanggal : ".$tanggal_mulai."\nJam : ".$jam."\nTempat : ".$tempat."
			\n\nJika terdapat kesalahan data Anda dapat menghubungi E-Camp Indonesia melalui:\nEmail : info@ecampindonesia.com\nTelp : 025451212\n\n-Terimakasih-";
			
			$this->email->from('fiian91@gmail.com', 'EcampIndonesia');
			$this->email->to($email);
			$this->email->subject('Notifikasi Pendaftaran E-Camp');
			$this->email->message($message);
	
			//$this->email->attach("file_pdf/pemesanan".$id_anggota.".pdf");
			$this->email->send();				
			//$this->email->print_debugger();
			//END KIRIM EMAIL NOTIFIKASI KE PESERTA
			
			
			
			
            $this->peserta_model->insert_peserta($data);
            if ($this->session->userdata('id_lisensi') == NULL) {
                //redirect('swalayan_home');
                // redirect('login');
				$data['title'] = 'Registrasi Berhasil';
				$data['email'] = $email;
				$this->template->write_view('header', 'template/header', $data);
				$this->template->write_view('content', 'sukses', $data);
				$this->template->write_view('footer', 'template/footer', $data);
				$this->template->render();
            } else {
                $this->session->set_userdata('id_batch', $this->input->post('batch'));
                redirect('peserta/peserta/daftar/' . $this->session->userdata('id_batch'));
            }
        }
    }

    
    public function edit($id) {
        $data['status'] = 'edit';
        $record_peserta = $this->peserta_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_peserta'] = $peserta->ID_PESERTA;
            $data['nama_peserta'] = $peserta->NAMA_PESERTA;
            $data['nama_panggilan'] = $peserta->NAMA_PANGGILAN;
            $data['jenis_kelamin'] = $peserta->JENIS_KELAMIN;
            $data['tempat_lahir'] = $peserta->TEMPAT_LAHIR;
            $data['tanggal'] = $peserta->TANGGAL;
            $data['bulan'] = $peserta->BULAN;
            $data['tahun'] = $peserta->TAHUN;
            $data['nama_usaha'] = $peserta->NAMA_USAHA;
            $data['jenis_usaha'] = $peserta->JENIS_USAHA;
            $data['omset'] = $peserta->OMSET;
            $data['pekerjaan'] = $peserta->PEKERJAAN;
            $data['no_hp'] = $peserta->NO_HP;
            $data['no_darurat'] = $peserta->NO_DARURAT;
            $data['hub_darurat'] = $peserta->HUB_DARURAT;
            $data['email'] = $peserta->EMAIL;
            $data['twitter'] = $peserta->TWITTER;
            $data['ukuran_baju'] = $peserta->UKURAN_BAJU;
            $data['alamat'] = $peserta->ALAMAT;
            $data['kode_pos'] = $peserta->KODE_POS;
            $data['id_kota'] = $peserta->ID_KOTA;
            $data['referensi'] = $peserta->REFERENSI;
            $data['no_transfer'] = $peserta->NO_TRANSFER;
			
			
			
            //$data['tanggal_pembayaran'] = $tanggal_pembayaran;
            $data['promo'] = $peserta->PROMO;
            $data['informasi'] = $peserta->INFORMASI;
            $data['back_link'] = '' . base_url() . 'index.php/peserta/peserta/daftar/' . $this->session->userdata('id_batch') . '';
            $data['id_batch'] = $peserta->ID_BATCH;
        }

        $id = 1;
        $data['batch'] = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
        $data['kota'] = $this->kota_model->get_all_kota();
        $data['title'] = 'Edit Peserta';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'peserta/form_peserta', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    //update peserta
    public function update_peserta($id) {
        if ($this->cek_validasi() == FALSE) {
            $this->edit($id);
        } else {
            $tahun = $this->input->post('thn');
            $bulan = $this->input->post('bln');
            $tanggal = $this->input->post('tgl');
            $tanggal_lahir = '' . $tahun . '-' . $bulan . '-' . $tanggal . '';
            $data = array(
                'NAMA_PESERTA' => $this->input->post('nama_peserta'),
                'NAMA_PANGGILAN' => $this->input->post('nama_panggilan'),
                'JENIS_KELAMIN' => $this->input->post('jenis_kelamin'),
                'TEMPAT_LAHIR' => $this->input->post('tempat_lahir'),
                'TANGGAL_LAHIR' => $tanggal_lahir,
                'NAMA_USAHA' => $this->input->post('nama_usaha'),
                'JENIS_USAHA' => $this->input->post('jenis_usaha'),
                'OMSET' => $this->input->post('omset'),
                'PEKERJAAN' => $this->input->post('pekerjaan'),
                'NO_HP' => $this->input->post('no_hp'),
                'NO_DARURAT' => $this->input->post('no_darurat'),
                'HUB_DARURAT' => $this->input->post('hub_darurat'),
                'EMAIL' => $this->input->post('email'),
                'TWITTER' => $this->input->post('twitter'),
                'UKURAN_BAJU' => $this->input->post('ukuran_baju'),
                'KODE_POS' => $this->input->post('kode_pos'),
                'ALAMAT' => $this->input->post('alamat'),
                'ID_KOTA' => $this->input->post('id_kota'),
                'REFERENSI' => $this->input->post('referensi'),
                'NO_TRANSFER' => $this->input->post('no_transfer'),
                'TANGGAL_PEMBAYARAN' => $this->input->post('tanggal_pembayaran'),
                'PROMO' => $this->input->post('promo'),
                'INFORMASI' => $this->input->post('informasi'),
                'ID_BATCH' => $this->input->post('batch')
                    //'ID_LISENSI'	 			=> 1,
                    //'STATUS'	 			=> 1
            );
            $this->peserta_model->update_peserta($id, $data);
            $this->session->set_userdata('id_batch', $this->input->post('batch'));
            redirect('peserta/peserta/daftar/' . $this->session->userdata('id_batch'));
        }
    }

    function cek_validasi() {
        // Setting Rules
        $this->form_validation->set_rules('nama_peserta', 'Nama Lengkap', 'required');
        $this->form_validation->set_rules('nama_panggilan', 'Nama Panggilan', 'required');
        $this->form_validation->set_rules('jenis_kelamin', 'Jenis Kelamin', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('alamat', 'Alamat Rumah', 'required');
        $this->form_validation->set_rules('kode_pos', 'Kode Pos', 'required|numeric');
        $this->form_validation->set_rules('id_kota', 'Kota', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('tempat_lahir', 'Tempat Lahir', 'required');
        $this->form_validation->set_rules('thn', 'Tanggal Lahir', 'required');
        $this->form_validation->set_rules('no_hp', 'No Telepon/Handphone', 'required|numeric');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('no_darurat', 'No Telepon Darurat', 'required|numeric');
        $this->form_validation->set_rules('hub_darurat', 'Hubungan dengan Pemilik Nomor Telepon Darurat', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('pekerjaan', 'Pekerjaan', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('batch', 'Angkatan/Batch', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('ukuran_baju', 'Ukuran T-Shirt', 'required|callback_cek_dropdown');
        $this->form_validation->set_rules('referensi', 'Referensi', 'required|callback_cek_dropdown');
        

        //Setting Error Message
        $this->form_validation->set_message('required', 'Kolom %s harus diisi.');
        $this->form_validation->set_message('numeric', 'Kolom %s harus diisi dengan karakter angka saja, desimal menggunakan titik (.)');
        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }

    //cek_dropdown
    function cek_dropdown($value) {
        if ($value === '0') {
            $this->form_validation->set_message('cek_dropdown', 'Kolom %s harus diisi.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function delete($id) {
        $spt_ids_post_array = split(",", $this->input->post('items'));
        //$this->load->model('pengolahan/data_model');
        //$msg='akun berhasil dihapus';
        foreach ($spt_ids_post_array as $index => $id) {
            if (isset($id) && $id != '') {
                $this->peserta_model->delete($id);
            }//end if
        }//end foreach
        //$this->peserta_model->delete($id);
        //redirect('peserta/peserta');
    }

    //status peserta
    public function status($id) {
        $records = $this->peserta_model->selectone($id);
        foreach ($records->result() as $row) {
            //set status peserta
            $status_baru = '';
            if ($row->STATUS_PESERTA == '0') {
                $data = array('STATUS_PESERTA' => 1);
                $this->peserta_model->update_peserta($id, $data);
                redirect('peserta/peserta/daftar/' . $this->session->userdata('id_batch'));
            } else if ($row->STATUS_PESERTA >= '1') {
                $data = array('STATUS_PESERTA' => 0);
                $this->peserta_model->update_peserta($id, $data);
                redirect('peserta/peserta/daftar/' . $this->session->userdata('id_batch'));
            }
        }
    }

    public function form_pembayaran($id) {
        $data['title'] = 'Pembayaran Peserta';
        $data['peserta'] = $this->peserta_model->selectone($id);
        $record_peserta = $this->peserta_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_peserta'] = $peserta->ID_PESERTA;
            $data['nama_peserta'] = $peserta->NAMA_PESERTA;
            $data['tanggal'] = $peserta->TANGGAL_BAYAR;
            $data['bulan'] = $peserta->BULAN_BAYAR;
            $data['tahun'] = $peserta->TAHUN_BAYAR;
            $data['bank'] = $peserta->BANK;
            $data['pembayaran_via'] = $peserta->PEMBAYARAN_VIA;
            $data['jumlah'] = $peserta->JUMLAH;
        }

        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'peserta/form_pembayaran', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    public function pembayaran($id) {
        $tahun = $this->input->post('thn');
        $bulan = $this->input->post('bln');
        $tanggal = $this->input->post('tgl');
        $tanggal_pembayaran = '' . $tahun . '-' . $bulan . '-' . $tanggal . '';
        $status = "";
        if ($this->input->post('jumlah') != NULL) {
            $status = 1;
        }
        $data = array(
            'TANGGAL_PEMBAYARAN' => $tanggal_pembayaran,
            'BANK' => $this->input->post('bank'),
            'PEMBAYARAN_VIA' => $this->input->post('pembayaran_via'),
            'JUMLAH' => $this->input->post('jumlah'),
            'STATUS_PESERTA' => $status,
        );
		$jumlah = $this->input->post('jumlah');
        $this->peserta_model->update_peserta($id, $data);
		//$this->insert_transaksi($id,$tanggal_pembayaran,$jumlah,$this->input->post('pembayaran_via'));
        redirect('peserta/peserta/daftar/' . $this->session->userdata('id_batch'));
    }

}

/* End of file pegawai.php */
/* Location: ./application/controllers/pegawai.php */